#!/bin/sh
#                        2001:db8:3::
# A                   R2--------------R3                     B
#        2001:db8:1::                      2001:db8:5::
#     R1                                                R6
#        2001:db8:2::                      2001:db8:6::
# C                   R4--------------R5                     D
#                         2001:db8:4::

ip netns add A
ip netns add B
ip netns add C
ip netns add D 
ip netns add R1
ip netns add R2
ip netns add R3
ip netns add R4
ip netns add R5
ip netns add R6
ip netns exec A ip link set lo up
ip netns exec B ip link set lo up
ip netns exec C ip link set lo up
ip netns exec D ip link set lo up
ip netns exec R1 ip link set lo up
ip netns exec R2 ip link set lo up
ip netns exec R3 ip link set lo up
ip netns exec R4 ip link set lo up
ip netns exec R5 ip link set lo up
ip netns exec R6 ip link set lo up


ip link add name A_R1 type veth peer name R1_A
ip link set A_R1 netns A 
ip link set R1_A netns R1
ip link add name C_R1 type veth peer name R1_C
ip link set C_R1 netns C
ip link set R1_C netns R1
ip netns exec A ip link set A_R1 up
ip netns exec A ip addr add 192.168.0.1/24 dev A_R1
ip netns exec A ip route add 10.0.0.0/24 via 192.168.0.254
ip netns exec C ip link set C_R1 up
ip netns exec C ip addr add 192.168.0.1/24 dev C_R1
ip netns exec C ip route add 10.0.0.0/24 via 192.168.0.254

ip link add name B_R6 type veth peer name R6_B
ip link set B_R6 netns B
ip link set R6_B netns R6
ip link add name D_R6 type veth peer name R6_D
ip link set D_R6 netns D
ip link set R6_D netns R6
ip netns exec B ip link set B_R6 up
ip netns exec B ip addr add 10.0.0.1/24 dev B_R6
ip netns exec B ip route add 192.168.0.0/24 via 10.0.0.254 dev B_R6
ip netns exec D ip link set D_R6 up
ip netns exec D ip addr add 10.0.0.1/24 dev D_R6
ip netns exec D ip route add 192.168.0.0/24 via 10.0.0.254 dev D_R6


ip link add name R1_R2 type veth peer name R2_R1
ip link add name R1_R4 type veth peer name R4_R1
ip link set R1_R2 netns R1
ip link set R1_R4 netns R1
ip link set R2_R1 netns R2
ip link set R4_R1 netns R4
ip netns exec R1 ip link set R1_R2 up
ip netns exec R1 ip addr add 2001:db8:1::12/64 dev R1_R2
ip netns exec R2 ip link set R2_R1 up
ip netns exec R2 ip addr add 2001:db8:1::21/64 dev R2_R1

ip netns exec R1 ip link set R1_R4 up
ip netns exec R1 ip addr add 2001:db8:2::14/64 dev R1_R4
ip netns exec R4 ip link set R4_R1 up
ip netns exec R4 ip addr add 2001:db8:2::41/64 dev R4_R1
ip netns exec R1 ip addr add fe80::1/64 dev R1_R2
ip netns exec R1 ip addr add fe80::1/64 dev R1_R4
ip netns exec R2 ip addr add fe80::2/64 dev R2_R1
ip netns exec R4 ip addr add fe80::4/64 dev R4_R1


ip netns exec R1 ip link add vif_ab type vrf table vab
ip netns exec R1 ip link add vif_cd type vrf table vcd
ip netns exec R1 ip link set vif_ab up
ip netns exec R1 ip link set vif_cd up
ip netns exec R1 ip link set R1_A up
ip netns exec R1 ip link set R1_C up
ip netns exec R1 ip link set R1_A master vif_ab
ip netns exec R1 ip link set R1_C master vif_cd
ip netns exec R1 ip addr add 192.168.0.254/24 dev R1_A
ip netns exec R1 ip addr add 192.168.0.254/24 dev R1_C
ip netns exec R1 sysctl net.ipv6.conf.all.forwarding=1
ip netns exec R1 sysctl net.ipv6.conf.all.seg6_enabled=1
ip netns exec R1 sysctl net.ipv6.conf.R1_A.seg6_enabled=1
ip netns exec R1 sysctl net.ipv6.conf.R1_C.seg6_enabled=1
ip netns exec R1 sysctl net.ipv6.conf.R1_R2.seg6_enabled=1
ip netns exec R1 sysctl net.ipv6.conf.R1_R4.seg6_enabled=1

ip netns exec R1 ip route add 10.0.0.0/24 encap seg6 mode encap segs D::2:21,D::3:32,D::6:6B dev R1_R2 table vab
ip netns exec R1 ip route add 10.0.0.0/24 encap seg6 mode encap segs D::4:41,D::5:54,D::6:6D dev R1_R4 table vcd
ip netns exec R1 ip -6 route add D::2:21 via fe80::2 dev R1_R2
ip netns exec R1 ip -6 route add D::4:41 via fe80::4 dev R1_R4


#R2 config
ip link add name R2_R3 type veth peer name R3_R2
ip link set R2_R3 netns R2
ip link set R3_R2 netns R3
ip netns exec R2 sysctl net.ipv6.conf.all.forwarding=1
ip netns exec R2 sysctl net.ipv6.conf.all.seg6_enabled=1
ip netns exec R2 sysctl net.ipv6.conf.R2_R3.seg6_enabled=1
ip netns exec R2 sysctl net.ipv6.conf.R2_R1.seg6_enabled=1

ip netns exec R2 ip addr add 2001:db8::3:23/64 dev R2_R3
ip netns exec R3 ip addr add 2001:db8::3:32/64 dev R3_R2

ip netns exec R2 ip addr add fe80::2/64 dev R2_R3
ip netns exec R3 ip addr add fe80::3/64 dev R3_R2

ip netns exec R2 ip link set R2_R3 up
ip netns exec R3 ip link set R3_R2 up

ip netns exec R2 ip -6 route add D::2:21 encap seg6local action End dev R2_R3
ip netns exec R2 ip -6 route add D::3:32 via fe80::3 dev R2_R3

#R3
ip link add name R3_R6 type veth peer name R6_R3
ip link set R3_R6 netns R3;
ip link set R6_R3 netns R6;
ip netns exec R3 sysctl net.ipv6.conf.all.forwarding=1;
ip netns exec R3 sysctl net.ipv6.conf.all.seg6_enabled=1
ip netns exec R3 sysctl net.ipv6.conf.R3_R2.seg6_enabled=1
ip netns exec R3 sysctl net.ipv6.conf.R3_R6.seg6_enabled=1

ip netns exec R3 ip addr add 2001:db8::5:36/64 dev R3_R6
ip netns exec R3 ip addr add fe80::3/64 dev R3_R6
ip netns exec R3 ip link set R3_R6 up
ip netns exec R3 ip -6 route add D::3:32 encap seg6local action End dev R3_R6
ip netns exec R3 ip -6 route add D::6:6B via fe80::6 dev R3_R6

#R6
# ip link add name R6_R5 type veth peer name R5_R6
# ip link set R6_R5 netns R6
# ip link set R5_R6 netns R5

# ip netns exec R6 sysctl net.ipv6.conf.all.forwarding=1;
# ip netns exec R6 sysctl net.ipv6.conf.all.seg6_enabled=1
# ip netns exec R6 sysctl net.ipv6.conf.R6_R3.seg6_enabled=1
# ip netns exec R6 sysctl net.ipv6.conf.R6_R5.seg6_enabled=1
# ip netns exec R6 sysctl net.ipv6.conf.R6_B.seg6_enabled=1
# ip netns exec R6 sysctl net.ipv6.conf.R6_D.seg6_enabled=1
# ip netns exec R6 sysctl net.vrf.strict_mode=1


# ip netns exec R6 ip link add vif_ab type vrf table vab
# ip netns exec R6 ip link add vif_cd type vrf table vcd
# ip netns exec R6 ip link set R6_B master vif_ab
# ip netns exec R6 ip link set R6_D master vif_cd
# ip netns exec R6 ip link set vif_ab up
# ip netns exec R6 ip link set vif_cd up

# ip netns exec R6 ip addr add 2001:db8::5:63/64 dev R6_R3
# ip netns exec R6 ip addr add 2001:db8::6:65/64 dev R6_R5
# ip netns exec R6 ip addr add fe80::6/64 dev R6_B
# ip netns exec R6 ip addr add fe80::6/64 dev R6_D
# ip netns exec R6 ip addr add fe80::6/64 dev R6_R5
# ip netns exec R6 ip addr add fe80::6/64 dev R6_R3
# ip netns exec R6 ip addr add 10.0.0.254/24 dev R6_B
# ip netns exec R6 ip addr add 10.0.0.254/24 dev R6_D
# ip netns exec R6 ip link set R6_B up
# ip netns exec R6 ip link set R6_D up
# ip netns exec R6 ip link set R6_R3 up

# ip netns exec R6 ip -6 route add D::6:6B encap seg6local action End.DT4 vrftable vab dev R6_B
# ip netns exec R6 ip route add 192.168.0.0/24 encap seg6 mode encap segs D::3:32,D::2:21,D::1:1A dev R6_R3 table vab

